//
//  ShareUI.m
//  ShareManager
//
//  Created by Jerry on 12/31/14.
//  Copyright (c) 2014 Jerry. All rights reserved.
//

#import "ShareUI.h"
#import "UIView+RSAdditions.h"
#import "UIColor+helper.h"
#define ShareWindowHeight 295
#ifndef SCREEN_WIDTH
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#endif
#ifndef SCREEN_HEIGHT
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#endif
@interface ShareUI ()<UIScrollViewDelegate>
@property (nonatomic, strong) NSBundle *bundle;
@property (nonatomic,strong) UIPageControl* pageController;
-(UIColor*) colorWithHex:(long)hexColor;
@end

@implementation ShareUI
+ (ShareUI *)sharedInstance
{
    static ShareUI   *sharedInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[ShareUI alloc] init];
    });
    return sharedInstance;
}

- (void)showShareWindowWithImageList:(NSArray *)shareImageList titleList:(NSArray *)shareTitleList tagList:(NSArray *)shareTagList
{

    UIView *shareWindowBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _shareWindowBackView = shareWindowBackView;
    _shareWindowBackView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.2];
    [[UIApplication sharedApplication].keyWindow addSubview:_shareWindowBackView];
    
    
    UIView *shareWindow = [[UIView alloc] initWithFrame:CGRectMake(5, SCREEN_HEIGHT, SCREEN_WIDTH-10, ShareWindowHeight)];
    _shareWindow = shareWindow;
    _shareWindow.backgroundColor = [UIColor clearColor];
    [_shareWindowBackView addSubview:_shareWindow];
    
    UIScrollView *shareWindowArea = [[UIScrollView alloc] initWithFrame:CGRectMake(0,_shareWindow.height-ShareWindowHeight-50, _shareWindow.width,ShareWindowHeight)];
    _shareWindowArea = shareWindowArea;
    _shareWindowArea.backgroundColor = [UIColor whiteColor];
    _shareWindowArea.showsHorizontalScrollIndicator=NO;
    _shareWindowArea.delegate=self;
    _shareWindowArea.showsVerticalScrollIndicator=NO;
    _shareWindowArea.layer.cornerRadius = 5;
    [_shareWindow addSubview:_shareWindowArea];
    
    UILabel *shareTitleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0,_shareWindow.height-ShareWindowHeight-40,SCREEN_WIDTH-20, 20)];
    shareTitleLbl.text =@"请选择分享平台";
    shareTitleLbl.textColor = [self colorWithHex:0x8f8f8f];
    shareTitleLbl.textAlignment = NSTextAlignmentCenter;
    shareTitleLbl.font = [UIFont systemFontOfSize:17];
    [_shareWindow addSubview:shareTitleLbl];
    
    
  
    for (int i=0; i<shareImageList.count; i++) {
        
        SMIcon *icon = [[SMIcon alloc] initWithTitle:shareTitleList[i] image:[UIImage imageNamed: shareImageList[i]]];
        icon.backgroundColor = [UIColor clearColor];
        if (i % 3 == 0) {
            icon.frame = CGRectMake((_shareWindowArea.width/3-58)/2+(i/6)*(SCREEN_WIDTH-10), 25+50+(i%6)/3*92, 58, 83);
        } else if (i % 3 == 1) {
            icon.frame = CGRectMake((_shareWindowArea.width-58)/2+(i/6)*(SCREEN_WIDTH-10), 25+50+(i%6)/3*92, 58, 83);
        }else if (i % 3 == 2) {
            icon.frame = CGRectMake(5*_shareWindowArea.width/6-58/2+(i/6)*(SCREEN_WIDTH-10), 25+50+(i%6)/3*92, 58, 83);
        }
        icon.tag = [shareTagList[i] integerValue];
        icon.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGuesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareAction:)];
        tapGuesture.numberOfTapsRequired = 1;
        [icon addGestureRecognizer:tapGuesture];
        [_shareWindowArea addSubview:icon];
    }
    int pages=(shareImageList.count%6)==0?(shareImageList.count/6):(shareImageList.count/6+1);
    
    _shareWindowArea.contentSize=CGSizeMake(pages*(SCREEN_WIDTH-10),_shareWindow.height-50 );
    _shareWindowArea.pagingEnabled=YES;
    
    _pageController = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 50, 15)];
    [_pageController setCenter:CGPointMake(SCREEN_WIDTH/2,_shareWindow.height-60)];
    
    _pageController.numberOfPages = 0;
    _pageController.currentPage = 0;  //157 199 250
    _pageController.pageIndicatorTintColor= [UIColor colorWithRed:157.0/255.0 green:199.0/255.0 blue:250/255.0 alpha:1];
    _pageController.currentPageIndicatorTintColor=[UIColor colorWithRed:0 green:98.0/255.0 blue:225/255.0 alpha:1];
    _pageController.numberOfPages=pages;
    if (pages==1) {
        _pageController.hidden=YES;
    }
    [_shareWindow addSubview:_pageController];
    
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(0, _shareWindow.height-45, _shareWindow.width, 40);
    [cancelBtn setTitle:@"取  消" forState:UIControlStateNormal];
    cancelBtn.titleLabel.font=[UIFont boldSystemFontOfSize:18.0f];
    
    [cancelBtn setTitleColor:[self colorWithHex:0x007aff] forState:UIControlStateNormal];
    cancelBtn.backgroundColor = [UIColor whiteColor];
    cancelBtn.layer.cornerRadius = 5;
    [cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [_shareWindow addSubview:cancelBtn];
    
    
    [UIView animateWithDuration:0.3 animations:^{
        _shareWindow.frame = CGRectMake(5, SCREEN_HEIGHT-ShareWindowHeight-5, SCREEN_WIDTH-10, ShareWindowHeight+5);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 animations:^{
            _shareWindow.frame = CGRectMake(5, SCREEN_HEIGHT-ShareWindowHeight, SCREEN_WIDTH-10, ShareWindowHeight);
        }];
    }];
}

- (void)cancelAction
{
    _shareWindowBackView.backgroundColor = [UIColor clearColor];
    [UIView animateWithDuration:0.3 animations:^{
        _shareWindowBackView.frame = CGRectMake(15, SCREEN_HEIGHT, SCREEN_WIDTH-30, ShareWindowHeight);
    } completion:^(BOOL finished) {
        [_shareWindowBackView removeFromSuperview];
    }];
}
- (void)shareAction:(UITapGestureRecognizer *)recognizer
{
    
    [_shareWindowBackView removeFromSuperview];
    if (self.delegate&&[self.delegate respondsToSelector:@selector(shareBtClickedWithTag:)]) {
        [self.delegate  shareBtClickedWithTag:recognizer.view.tag];
    }
        
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView.contentOffset.x >0&&(_pageController.numberOfPages >(scrollView.contentOffset.x/(SCREEN_WIDTH-10)))) {
        _pageController.currentPage=scrollView.contentOffset.x/(SCREEN_WIDTH-10);
    }
}
-(UIColor*) colorWithHex:(long)hexColor {
    return [self colorWithHex:hexColor alpha:1.0];
}

-(UIColor *)colorWithHex:(long)hexColor alpha:(float)opacity {
    float red = ((float)((hexColor & 0xFF0000) >> 16))/255.0;
    float green = ((float)((hexColor & 0xFF00) >> 8))/255.0;
    float blue = ((float)(hexColor & 0xFF))/255.0;
    return [UIColor colorWithRed:red green:green blue:blue alpha:opacity];
}

@end
